package org.millioncall.yueyoga.admin.action;



import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.util.StringUtils;
import org.millioncall.openplatform.util.WebAppUtils;
import org.millioncall.yueyoga.admin.model.Chedule;
import org.millioncall.yueyoga.admin.model.Consume;
import org.millioncall.yueyoga.admin.model.Group;
import org.millioncall.yueyoga.admin.model.Member;
import org.millioncall.yueyoga.admin.model.MemberClazz;
import org.millioncall.yueyoga.admin.model.ProductRemaining;
import org.millioncall.yueyoga.admin.service.CheduleService;
import org.millioncall.yueyoga.admin.service.ConsumptionService;
import org.millioncall.yueyoga.admin.service.MemberClazzService;
import org.millioncall.yueyoga.admin.service.MemberService;
import org.millioncall.yueyoga.admin.service.ProductRemainingService;
import org.millioncall.yueyoga.admin.service.ProductService;
import org.millioncall.yueyoga.admin.service.RemainingService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author lisj Mar 15, 2014 1:35:37 PM
 */
@Controller
public class ConsumptionAction extends BaseAction {
	private ConsumptionService consumptionService;
	private ProductRemainingService productRemainingService;
	private MemberService memberService;
	private ProductService productService;
	private RemainingService remainingService;
	private CheduleService cheduleService;
	private MemberClazzService memberClazzService;

	public ConsumptionService getConsumptionService() {
		return consumptionService;
	}

	public void setConsumptionService(ConsumptionService consumptionService) {
		this.consumptionService = consumptionService;
	}

	public ProductRemainingService getProductRemainingService() {
		return productRemainingService;
	}

	public void setProductRemainingService(
			ProductRemainingService productRemainingService) {
		this.productRemainingService = productRemainingService;
	}

	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public ProductService getProductService() {
		return productService;
	}

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	public RemainingService getRemainingService() {
		return remainingService;
	}

	public void setRemainingService(RemainingService remainingService) {
		this.remainingService = remainingService;
	}
	
	public CheduleService getCheduleService() {
		return cheduleService;
	}

	public void setCheduleService(CheduleService cheduleService) {
		this.cheduleService = cheduleService;
	}

	public MemberClazzService getMemberClazzService() {
		return memberClazzService;
	}

	public void setMemberClazzService(MemberClazzService memberClazzService) {
		this.memberClazzService = memberClazzService;
	}

	/** 跳转到消费页面 */
	@RequestMapping(value="/consumption.jhtml")
	public String consumption(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		super.initFrontHeader(request, model);
		return "consumption";
	}
	
	/** 点击剩余产品复选框时显示对应的班级课程 */
	@RequestMapping(value="findConsumptionWhenClickCheckbox.jhtml")
	public void findConsumptionWhenClickCheckbox(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		String msg = "";
		
		String strStart = request.getParameter("start");
		String strLimit = request.getParameter("limit");
		String strProduct = request.getParameter("productId");
		
		//每页5条
		int start = 0, limit = 5, productId = 0;
		
		try {
			start = Integer.parseInt(strStart);
			limit = Integer.parseInt(strLimit);
		} catch (NumberFormatException e) {
			start = 0;
			limit = 5;
		}
		
		if (start <= 0) start = 1;
		start = (start - 1) * limit;
		
		//获得剩余产品编号
		try {
			productId = Integer.parseInt(strProduct);
		} catch (NumberFormatException e) {
			msg = "{'success':false, 'msg':'剩余产品编号不正确！'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}
		
		//获得剩余产品
		ProductRemaining pr = this.productRemainingService.findProductRemainingById(productId);
		Map<String, Object> map = new HashMap<String, Object>();
		Calendar calendar = Calendar.getInstance();
		int week = calendar.get(Calendar.DAY_OF_WEEK);
		if (week == 1) week = 7;
		else if (week == 2) week = 1;
		else if (week == 3) week = 2;
		else if (week == 4) week = 3;
		else if (week == 5) week = 4;
		else if (week == 6) week = 5;
		else if (week == 7) week = 6;
		
		//查找全部可用的消费组装参数
		map.put("week", week);
		map.put("productId", pr.getProduct().getProductId());
		map.put("now", new Date());
		//查找今天全部课程
		PaginatedList<Chedule> cheList = this.cheduleService.findConsumptionSchedule(map, start, limit);
		
		if (cheList.size() <= 0) {
			msg = "{'success':false, 'msg':'未找到课程！'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}
		
		StringBuffer sb = new StringBuffer("[");
		String strWeek = "";
		String strET = "";
		Date endTime = null;
		Date now = new Date();
		String strNow = "";
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
		for (Chedule schedule : cheList) {
			sb.append("{");
			sb.append("'scheduleId':'" + schedule.getId() + "',");
			sb.append("'classId':'" + schedule.getClazz().getId() + "',");
			sb.append("'className':'" + schedule.getClazz().getName() + "',");
			sb.append("'scheduleStarttime':'" + schedule.getStarttime() + "',");
			sb.append("'scheduleEndtime':'" + schedule.getEndtime() + "',");
			sb.append("'coachName':'" + schedule.getTeacher().getName() + "',");
			sb.append("'assistantName':'" + schedule.getAssistant().getName() + "',");
			sb.append("'placeName':'" + schedule.getPlace().getName() + "',");
			strET = schedule.getEndtime();
			try {
				endTime = sdf.parse(strET);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			strNow = sdf.format(now);
			try {
				now = sdf.parse(strNow);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (now.after(endTime)) {
				sb.append("'expired':'" + 1 + "',");
			} else {
				sb.append("'expired':'" + 0 + "',");
			}
			if (schedule.getWeek() == 1) strWeek = "周一";
			else if (schedule.getWeek() == 2) strWeek = "周二";
			else if (schedule.getWeek() == 3) strWeek = "周三";
			else if (schedule.getWeek() == 4) strWeek = "周四";
			else if (schedule.getWeek() == 5) strWeek = "周五";
			else if (schedule.getWeek() == 6) strWeek = "周六";
			else if (schedule.getWeek() == 7) strWeek = "周日";
			sb.append("'week':'" + strWeek + "',");
			sb.append("'isReservation':'" + schedule.getIsReservation() + "',");
			sb.append("'classType':'" + schedule.getClazz().getType() + "'},");
		}
		
		String str = sb.toString();
		str = str.substring(0, str.length() - 1);
		str += "]";
		msg = "{'success':true, 'totalPages':'" + cheList.getPager().getCountOfTotalPages() + "', 'totalResults':'" + cheList.getPager().getCountOfTotalResults() + "', 'items':" + str + "}";
		
		WebAppUtils.ajaxResponse(response, msg);
	}
	
	/** 查询消费会员的剩余产品和班级课程 */
	@RequestMapping(value="/searchOneConsumption.jhtml")
	public String searchConsumption(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		super.initFrontHeader(request, model);
		
		model.put("resultBool", true);
		
		String cardNumber = request.getParameter("cardNumber");
		String strPrId = request.getParameter("prId");
		Member mem = null;
		Map<String, Object> mapM = new HashMap<String, Object>();
		Map<String, Object> mapPR = new HashMap<String, Object>();
		Map<String, Object> mapCC = new HashMap<String, Object>();
		
		List<Integer> groupIds = new ArrayList<Integer>();
		for (Group group : this.getUser(request).getGroups()) {
			groupIds.add(group.getId());
		}
		
		String strPRStart = request.getParameter("pagePRNo");
		String strCCStart = request.getParameter("pageCCNo");
		
		int prStart = 1, ccStart = 1, limit = 5;
		try {
			prStart = Integer.parseInt(strPRStart);
			ccStart = Integer.parseInt(strCCStart);
		} catch (NumberFormatException e) {
			prStart = 1;
			ccStart = 1;
		}
		
		if (prStart < 1) prStart = 1;
		if (ccStart < 1) ccStart = 1;
		prStart = (prStart - 1) * limit;
		ccStart = (ccStart - 1) * limit;
		
		//卡号不可空
		if (StringUtils.isBlank(cardNumber)) {
			model.put("resultBool", false);
			model.put("result", "卡号不能为空");
			return "consumption";
		}
		
		//查找会员
		mapM.put("groups", groupIds);
		mapM.put("columnName", "cardNumber");
		mapM.put("value", cardNumber);
		mapM.put("hallcode", this.getUser(request).getOrganizationId());
		mem = this.memberService.findByCardNumber(mapM);
		
		if (mem == null) {
			model.put("resultBool", false);
			model.put("result", "未找到会员");
			return "consumption";
		}
		model.put("member", mem);
		
		//查找剩余产品
		mapPR.put("groups", groupIds);
		mapPR.put("cardNumber", cardNumber);
		PaginatedList<ProductRemaining> prList = this.productRemainingService.findConsumptionProductRemaining(mapPR, prStart, limit);
		model.put("prList", prList);
		
		model.put("pageProductId", null);
		//查找班级课程
		PaginatedList<Chedule> cheList = null;
		if (prList.size() > 0) {
			ProductRemaining pr = null;
			if (StringUtils.isBlank(strPrId)) {
				pr = prList.get(0);
				mapCC.put("productId", pr.getProduct().getProductId());
				model.put("pageProductId", pr.getId());
			} else {
				Integer prId = 0;
				try {
					prId = Integer.parseInt(strPrId);
				} catch (NumberFormatException e) {
					model.put("resultBool", false);
					model.put("result", "未找到剩余产品");
					return "consumption";
				}
				pr = this.productRemainingService.findProductRemainingById(prId);
				mapCC.put("productId", pr.getProduct().getProductId());
				model.put("pageProductId", pr.getId());
			}
			Calendar calendar = Calendar.getInstance();
			int week = calendar.get(Calendar.DAY_OF_WEEK);
			if (week == 1) week = 7;
			else if (week == 2) week = 1;
			else if (week == 3) week = 2;
			else if (week == 4) week = 3;
			else if (week == 5) week = 4;
			else if (week == 6) week = 5;
			else if (week == 7) week = 6;
			
			//查找全部可用的消费组装参数
			mapCC.put("week", week);
			mapCC.put("now", new Date());
			//查找全部可用的消费并分页
			cheList = this.cheduleService.findConsumptionSchedule(mapCC, ccStart, limit);
			
			Date now = new Date();
			String strNow = "";
			String strST = null;
			Date startTime = null;
			SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
			for (Chedule sche : cheList) {
				strNow = sdf.format(now);
				try {
					now = sdf.parse(strNow);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				strST = sche.getStarttime();
				try {
					startTime = sdf.parse(strST);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if (now.after(startTime)) {
					sche.setExpired(1);
				} else {
					sche.setExpired(0);
				}
			}
		}
		if(cheList == null){
			model.put("pageTotalCCPages", 0);
		}else{
			model.put("pageTotalCCPages", cheList.getPager().getCountOfTotalPages());
		}
		//页面展示课程
		model.put("ccList", cheList);
		
		return "consumption";
	}
	
	/** 添加消费 */
	@RequestMapping(value = "addConsume.jhtml")
	public void addConsumption(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		String msg = "";
		//获得会员编号
		String strMemberId = request.getParameter("memberId");
		//获得剩余产品编号
		String strPrId = request.getParameter("prId");
		//获得课程编号
		String strScheId = request.getParameter("scheduleId");
		//新旧课程标识
		String strIsReservation = request.getParameter("isReservation");
		
		int isReservation = 5;
		try {
			isReservation = Integer.parseInt(strIsReservation);
		} catch (NumberFormatException e) {
			msg = "{'success':false, 'msg':'新旧课程编号不正确！'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}
		//判断几天有没有来上过课
		Map<String, Object> mapConsumed = new HashMap<String, Object>();
		mapConsumed.put("memberId", strMemberId);
		mapConsumed.put("scheduleId", strScheId);
		mapConsumed.put("now", new Date());
		mapConsumed.put("isReservation", isReservation);
		boolean b = this.consumptionService.isConsumed(mapConsumed);
		if (b) {
			msg = "{'success':false, 'msg':'该会员今天已经消费过了！'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}
		
		Member mem = null;
		ProductRemaining pr = null;
		Chedule schedule = null;
		int memberId = 0, prId = 0, scheduleId = 0;
		try {
			memberId = Integer.parseInt(strMemberId);
		} catch (NumberFormatException e) {
			msg = "{'success':false, 'msg':'会员编号不正确！'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		List<Integer> groupIds = new ArrayList<Integer>();
		for (Group group : this.getUser(request).getGroups()) {
			groupIds.add(group.getId());
		}
		map.put("groups", groupIds);
		map.put("id", memberId);
		map.put("hallcode", this.getUser(request).getOrganizationId());
		mem = this.memberService.findById(map);
		
		//如果没有此会员
		if (mem == null) {
			msg = "{'success':false, 'msg':'未找到此会员！'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}
		
		try {
			prId = Integer.parseInt(strPrId);
		} catch (NumberFormatException e) {
			msg = "{'success':false, 'msg':'剩余产品编号不正确！'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}
		pr = this.productRemainingService.findProductRemainingById(prId);
		
		//如果没有此产品
		if (pr == null) {
			msg = "{'success':false, 'msg':'未找到该剩余产品，请确保您购买了此产品！'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}
		
		//如果没有此产品
		if (pr.getProduct() == null) {
			msg = "{'success':false, 'msg':'未找到该产品，该产品可能已下架！'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}
		
		try {
			scheduleId = Integer.parseInt(strScheId);
		} catch (NumberFormatException e) {
			msg = "{'success':false, 'msg':'课程编号不正确！'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}
		
		Map<String, Object> mapScheduleNew = new HashMap<String, Object>();
		mapScheduleNew.put("scheduleId", scheduleId);
		mapScheduleNew.put("isReservation", isReservation);
		schedule = this.cheduleService.findConsumptionNewById(mapScheduleNew);
		
		if (schedule == null)
			schedule = this.cheduleService.getCheduleById(scheduleId);
		
		//如果没有此课程
		if (schedule == null) {
			msg = "{'success':false, 'msg':'未找到此课程！'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}
		
		//是否是固定会员班级
		if ("固定会员".equals(schedule.getClazz().getType())) {
			//如果是，看该会员是否属于该班级
			Map<String, Object> mapMC = new HashMap<String, Object>();
			mapMC.put("memberId", mem.getMemberid());
			mapMC.put("classId", schedule.getClazz().getId());
			List<MemberClazz> list = this.memberClazzService.findAllMemberInThisClazz(mapMC);
			if (list.size() <= 0) {
				msg = "{'success':false, 'msg':'会员不在班级内！'}";
				WebAppUtils.ajaxResponse(response, msg);
				return;
			}
		}
		
		//新建消费
		Consume con = new Consume();
		con.setClazz(schedule.getClazz());
		con.setClazzId(schedule.getClazz().getId());
		con.setClazzName(schedule.getClazz().getName());
		con.setConsumeTime(new Date());
		con.setCreatedBy(this.getUser(request));
		con.setCreatedById(this.getUser(request).getId());
		con.setCreateTime(new Date());
		con.setInputTime(new Date());
		con.setMember(mem);
		con.setMemberId(mem.getMemberid());
		con.setMemberName(mem.getName());
		con.setPlace(schedule.getPlace());
		con.setPlaceId(schedule.getPlace().getId());
		con.setPlaceName(schedule.getPlace().getName());
		con.setProduct(pr.getProduct());
		con.setProductId(pr.getProduct().getProductId());
		con.setProductName(pr.getProduct().getName());
		con.setSchedule(schedule);
		con.setScheduleAssistant(schedule.getAssistant());
		con.setScheduleAssistantId(schedule.getAssistant().getCoachId());
		con.setScheduleAssistantName(schedule.getAssistant().getName());
		con.setScheduleEndTime(schedule.getEndtime());
		con.setScheduleId(schedule.getId());
		con.setScheduleStartTime(schedule.getStarttime());
		con.setScheduleTeacher(schedule.getTeacher());
		con.setScheduleTeacherId(schedule.getTeacher().getCoachId());
		con.setScheduleTeacherName(schedule.getTeacher().getName());
		con.setWeek(schedule.getWeek());
		con.setIsReservation(isReservation);
		
		String res = this.consumptionService.saveConsume(con, pr, mem);
		if ("true".equals(res)) {
			msg = "{'success':true,'msg':'本次消费成功!'}";
		} else {
			if (StringUtils.isBlank(res)) {
				msg = "{'success':false,'msg':'本次消费失败!'}";
			} else {
				msg = res;
			}
		}
		
		WebAppUtils.ajaxResponse(response, msg);
	}
	
	
	
	/*public void addConsumption(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		String msg = null;
		//获得会员编号
		String strMemberId = request.getParameter("memberId");
		//获得会员名称
		String memberName = request.getParameter("member.name");
		
		//获得班级编号
		String strClazzId = request.getParameter("clazzSelectedId");
		//获得班级名称
		String clazzName = request.getParameter("clazzSelectedName");
		
		//获得课程编号
		String strScheduleId = request.getParameter("scheduleSelectedId");
		//获得课程开始时间
		String strStartTime = request.getParameter("scheduleSelectedStartTime");
		//获得课程结束时间
		String strEndTime = request.getParameter("scheduleSelectedEndTime");
		
		//获得课程教练编号
		String strTeacherId = request.getParameter("scheduleSelectedTeacherId");
		//获得课程教练名称
		String teacherName = request.getParameter("scheduleSelectedTeacherName");
		
		//获得课程助教编号
		String strAssistantId = request.getParameter("scheduleSelectedAssistantId");
		//获得课程助教名称
		String assistantName = request.getParameter("scheduleSeletedAssistantName");
		
		//获得课程场地编号
		String strPlaceId = request.getParameter("scheduleSelectedPlaceId");
		//获得课程场地名称
		String placeName = request.getParameter("scheduleSelectedPlaceName");
		
		//获得剩余产品编号
		String strProductRemainingId = request.getParameter("productSelectedRemainingId");
		
		//周几
		String strWeek = request.getParameter("scheduleSelectedWeek");
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		Consume con = new Consume();
		
		//会员编号
		int memberId = 0;
		if (StringUtils.isBlank(strMemberId)) {
			msg = "{'success':false,'msg':'会员编号不能为空!'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		} else {
			try {
				memberId = Integer.parseInt(strMemberId);
			} catch (NumberFormatException e) {
				msg = "{'success':false,'msg':'会员编号格式不正确!'}";
				WebAppUtils.ajaxResponse(response, msg);
				return;
			}
		}
		con.setMemberId(memberId);
		Map<String, Object> map1 = new HashMap<String, Object>();
		List<Integer> groupIds1 = new ArrayList<Integer>();
		for (Group group : this.getUser(request).getGroups()) {
			groupIds1.add(group.getId());
		}
		map1.put("groups", groupIds1);
		map1.put("id", memberId);
		Member member = this.memberService.findById(map1);
		
		//会员名称
		if (StringUtils.isBlank(memberName)) {
			msg = "{'success':false,'msg':'会员名称不能为空!'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}
		con.setMemberName(memberName);
		
		//班级编号
		int clazzId = 0;
		if (StringUtils.isBlank(strClazzId)) {
			msg = "{'success':false,'msg':'班级编号不能为空!'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		} else {
			try {
				clazzId = Integer.parseInt(strClazzId);
			} catch (NumberFormatException e) {
				msg = "{'success':false,'msg':'班级编号格式不正确!'}";
				WebAppUtils.ajaxResponse(response, msg);
				return;
			}
		}
		con.setClazzId(clazzId);
		
		//班级名称
		if (StringUtils.isBlank(clazzName)) {
			msg = "{'success':false,'msg':'班级名称不能为空!'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}
		con.setClazzName(clazzName);
		
		//课程编号
		int scheduleId = 0;
		if (StringUtils.isBlank(strScheduleId)) {
			msg = "{'success':false,'msg':'课程编号不能为空!'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		} else {
			try {
				scheduleId = Integer.parseInt(strScheduleId);
			} catch (NumberFormatException e) {
				msg = "{'success':false,'msg':'课程编号格式不正确!'}";
				WebAppUtils.ajaxResponse(response, msg);
				return;
			}
		}
		con.setScheduleId(scheduleId);
		
		//课程开始时间
		if (StringUtils.isBlank(strStartTime)) {
			msg = "{'success':false,'msg':'课程开始时间不能为空!'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}
		con.setScheduleStartTime(strStartTime);
		
		//课程结束时间
		if (StringUtils.isBlank(strEndTime)) {
			msg = "{'success':false,'msg':'课程结束时间不能为空!'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}
		con.setScheduleEndTime(strEndTime);
		
		//课程教练编号
		int teacherId = 0;
		if (StringUtils.isBlank(strTeacherId)) {
			msg = "{'success':false,'msg':'教练编号不能为空!'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		} else {
			try {
				teacherId = Integer.parseInt(strTeacherId);
			} catch (NumberFormatException e) {
				msg = "{'success':false,'msg':'教练编号格式不正确!'}";
				WebAppUtils.ajaxResponse(response, msg);
				return;
			}
		}
		con.setScheduleTeacherId(teacherId);
		
		//课程教练名称
		if (StringUtils.isBlank(teacherName)) {
			msg = "{'success':false,'msg':'教练名称不能为空!'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}
		con.setScheduleTeacherName(teacherName);
		
		//课程助教编号
		int assistantId = 0;
		if (StringUtils.isBlank(strAssistantId)) {
			msg = "{'success':false,'msg':'助教编号不能为空!'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		} else {
			try {
				assistantId = Integer.parseInt(strAssistantId);
			} catch (NumberFormatException e) {
				msg = "{'success':false,'msg':'助教编号格式不正确!'}";
				WebAppUtils.ajaxResponse(response, msg);
				return;
			}
		}
		con.setScheduleAssistantId(assistantId);
		
		//课程助教名称
		if (StringUtils.isBlank(assistantName)) {
			msg = "{'success':false,'msg':'助教名称不能为空!'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}
		con.setScheduleAssistantName(assistantName);
		
		//课程场地编号
		int placeId = 0;
		if (StringUtils.isBlank(strPlaceId)) {
			msg = "{'success':false,'msg':'场地编号不能为空!'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		} else {
			try {
				placeId = Integer.parseInt(strPlaceId);
			} catch (NumberFormatException e) {
				msg = "{'success':false,'msg':'场地编号格式不正确!'}";
				WebAppUtils.ajaxResponse(response, msg);
				return;
			}
		}
		con.setPlaceId(placeId);
		
		//课程场地名称
		if (StringUtils.isBlank(placeName)) {
			msg = "{'success':false,'msg':'场地名称不能为空!'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}
		con.setPlaceName(placeName);
		
		//剩余产品编号
		int productRemainingId = 0;
		if (StringUtils.isBlank(strProductRemainingId)) {
			msg = "{'success':false,'msg':'产品编号不能为空!'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		} else {
			try {
				productRemainingId = Integer.parseInt(strProductRemainingId);
			} catch (NumberFormatException e) {
				msg = "{'success':false,'msg':'产品编号格式不正确!'}";
				WebAppUtils.ajaxResponse(response, msg);
				return;
			}
		}
		//找到会员的剩余产品
		ProductRemaining pr = this.productRemainingService.findProductRemainingById(productRemainingId);
		
		Product product = pr.getProduct();
		
		con.setProductId(product.getProductId());
		
		//产品名称
		con.setProductName(product.getName());
		
		//判断产品是否过期
		
		if (product.getBeginTime() == null) {
			
			if (101 == product.getType()) {
				
			} else if (102 == product.getType()) {		//时间区域卡
				
			} else if (103 == product.getType()) {		//年卡
				product.setBeginTime(new Date());
			} else if (104 == product.getType()) {		//季卡
				product.setBeginTime(new Date());
			} else if (105 == product.getType()) {		//月卡
				product.setBeginTime(new Date());
			} else if (106 == product.getType()) {		//半月卡
				product.setBeginTime(new Date());
			} else if( 107 == product.getType()) {		//金额卡
				
			} else {									//实时扣费
				
			}
		}
		
		if (product.getEndTime() == null) {
			//如果为此卡
			if (101 == product.getType()) {
				
			} else if (102 == product.getType()) {		//时间区域卡
				
			} else if (103 == product.getType()) {		//年卡
				
			} else if (104 == product.getType()) {		//季卡
				
			} else if (105 == product.getType()) {		//月卡
				
			} else if (106 == product.getType()) {		//半月卡
				
			} else if( 107 == product.getType()) {		//金额卡
				
			} else {									//实时扣费
				
			}
			date = new Date();
			if (date.after(product.getEndTime())) {
				msg = "{'success':false,'msg':'产品已过期!'}";
				WebAppUtils.ajaxResponse(response, msg);
				return;
			}
		}
		
		//计算消费金额
		//次卡
		if (101 == product.getType()) {
			//获得剩余产品的可用次数
			int count = pr.getCounts();
			if (count <= 0) {
				msg = "{'success':false,'msg':'次卡次数已不足!'}";
				WebAppUtils.ajaxResponse(response, msg);
				return;
			}
			count--;
			map.put("counts", count);
			map.put("productRemainingId", pr.getId());
			boolean flag = productRemainingService.updateProductRemaining(map);
			if (!flag) {
				msg = "{'success':true,	'msg':'次卡数量更新失败!'}";
				WebAppUtils.ajaxResponse(response, msg);
				return;
			}
		} else if (107 == product.getType() || 108 == product.getType()) { // 此状态单独消费该产品类型
			Remaining remaining = remainingService.findById(Integer.valueOf(memberId));
			Double money = remaining.getRemaining();
			Double proPrice = product.getPrice();
			// 金额计算
			money = NumberUtils.sub(money, proPrice);
			if (money < 0) {
				msg = "{'success':false,'msg':'您的金额已不足,请及时充值!'}";
				WebAppUtils.ajaxResponse(response, msg);
				return;
			} else {
				remaining.setRemaining(money);
				remainingService.updateRemainingById(remaining);
			}
		} else {
			Date date = new Date();
			if (pr.getEndTime() == null) {
				
			} else {
				if (date.after(pr.getEndTime())) {
					msg = "{'success':false,'msg':'产品已过期!'}";
					WebAppUtils.ajaxResponse(response, msg);
					return;
				}
			}
		}
		
		//周几
		con.setWeek(Integer.parseInt(strWeek));
		
		con.setCreateTime(new Date());
		con.setConsumeTime(new Date());
		User loginUser = this.getUser(request);
		con.setCreatedBy(loginUser);
		con.setCreatedById(loginUser.getId());
		
		if (consumptionService.saveConsume(con)) {
			member.setSubstatus("0021");
			
			if (memberService.updateMember(member)) {
				msg = "{'success':true,	'msg':'本次消费成功!'}";
			} else {
				msg = "{'success': 'false', 'msg': '更新会员状态失败!'}";
				WebAppUtils.ajaxResponse(response, msg);
				return;
			}
		} else {
			msg = "{'success':false,'msg':'本次消费失败!'}";
		}
		
		WebAppUtils.ajaxResponse(response, msg);
	}*/
	
	
	
}
